#include "Cbackgroundwidget.h"
#include "ui_Cbackgroundwidget.h"

#include <QScreen>
#include <QPushButton>
#include <QPainter>
#include <QLabel>
#include <QMessageBox>
#include <QBitmap>

CbackgroundWidget::CbackgroundWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CbackgroundWidget)
{
    ui->setupUi(this);

    initUI();

    setAttribute(Qt::WA_DeleteOnClose);
    setAttribute(Qt::WA_TranslucentBackground,true);
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
}

void CbackgroundWidget::initUI() {
    ui->horizontalView->setStyleSheet("background-color: rgb(28, 30, 32);border-radius: 5px;");

    //mic
    ui->Button_Mute->setButtonTxt("mute");
    ui->Button_Mute->setNormalImg("new/icon/image/icon_toolbar_unmute.png");
    ui->Button_Mute->setPressImg("new/icon/image/icon_toolbar_mute.png");
    ui->Button_Mute->needSelect = true;

    //camera
    ui->Button_Camera->setButtonTxt("stop camera");
    ui->Button_Camera->setNormalImg("new/icon/image/icon_toolbar_unmute-camera.png");
    ui->Button_Camera->setPressImg("new/icon/image/icon_toolbar_mute-camera.png");
    ui->Button_Camera->needSelect = true;

    //manager
    ui->Button_Manager->setButtonTxt("manager");
    ui->Button_Manager->setNormalImg("new/icon/image/icon_toolbar_detail.png");
    ui->Button_Manager->setPressImg("new/icon/image/icon_detail_c.png");

    //drawing
    ui->Button_Draw->setButtonTxt("save");
    ui->Button_Draw->setNormalImg("new/icon/image/icon_brush_default.png");
    ui->Button_Draw->setPressImg("new/icon/image/icon_brush_click.png");

    //save
    ui->Button_Save->setButtonTxt("save");
    ui->Button_Save->setNormalImg("new/icon/image/icon_save_default.png");
    ui->Button_Save->setPressImg("new/icon/image/icon_save_click.png");

    //stop
    ui->Button_Close->setButtonTxt("stop");
    ui->Button_Close->setNormalImg("new/icon/image/icon_exit_default.png");
    ui->Button_Close->setPressImg ("new/icon/image/icon_exit_click.png");

    //view
    paintListWidget = new PaintListWidget(this);
    paintListWidget->hide();

    colorWidget = new ColorWidget(this);
    colorWidget->hide();

    selectBgColorWidget = new SelectBgColorWidget(this);
    selectBgColorWidget->hide();

    //SiGNAL
    connect(ui->Button_Save, SIGNAL(clicked()), this, SLOT(saveImg()));
    connect(ui->Button_Close, SIGNAL(clicked()), this, SLOT(stopShare()));
    connect(ui->shareBtnView, SIGNAL(mySignal()), this, SLOT(entershareBtn()));
    connect(ui->Button_Draw, SIGNAL(clicked()), this, SLOT(showPaintWindow()));

    connect(paintListWidget, SIGNAL(penClicked()), this, SLOT(onBtnPenClicked()));
    connect(paintListWidget, SIGNAL(nitePenClicked()), this, SLOT(onBtnNitePenClicked()));
    connect(paintListWidget, SIGNAL(eraserClicked()), this, SLOT(onBtnEraserClicked()));
    connect(paintListWidget, SIGNAL(revokeClicked()), this, SLOT(onBtnRevokeClicked()));
    connect(paintListWidget, SIGNAL(bgClicked()), this, SLOT(onBtnBgClicked()));
    connect(paintListWidget, SIGNAL(clearClicked()), this, SLOT(onBtnClearClicked()));
    connect(paintListWidget, SIGNAL(exitClicked()), this, SLOT(onBtnExitClicked()));
    connect(paintListWidget, SIGNAL(selectPenClicked()), this, SLOT(onBtnSelectPenClicked()));
    connect(paintListWidget, SIGNAL(selectNitePenClicked()), this, SLOT(onBtnSelectNitePenClicked()));

    connect(colorWidget, SIGNAL(changeColor()), this, SLOT(changePenColor()));
    connect(colorWidget, SIGNAL(changeSize()), this, SLOT(changePenLineSize()));

    connect(selectBgColorWidget, SIGNAL(changeDrawBg(QString)), this, SLOT(c_changeDrawBg(QString)));
}

void CbackgroundWidget::AllPropertyWidgetHide() {
    colorWidget->hide();

    selectBgColorWidget->hide();
}

void CbackgroundWidget::onBtnPenClicked() {

    AllPropertyWidgetHide();

    QColor c = colorWidget->GetColor();
    c.setAlpha(255);
    QPen pen(c,colorWidget->GetSize());
    ui->paintWidget->setPen(pen);

    ui->w->setStyleSheet("#w{background-color: rgb(190,190,190,1)}");
    setCursor(QCursor(QPixmap(":/new/icon/image/icon_brush_click.png")));

    ui->paintWidget->setModelType(PW::ModelType::FREELINE);
}

void CbackgroundWidget::onBtnNitePenClicked() {
    AllPropertyWidgetHide();

    QColor c = colorWidget->GetColor();
    c.setAlpha(80);
    QPen pen(c,colorWidget->GetSize());
    ui->paintWidget->setPen(pen);

    setCursor(QCursor(QPixmap(":/new/icon/image/icon_highlighter_click.png")));
    ui->paintWidget->setModelType(PW::ModelType::NITELINE);
}

void CbackgroundWidget::onBtnEraserClicked() {

    AllPropertyWidgetHide();

    setCursor(QCursor(QPixmap(":/new/icon/image/icon_eraser_click.png")));

    QPen pen(ui->paintWidget->pen.color(),9);
    ui->paintWidget->setPen(pen);

    if (isLucency) {
        ui->paintWidget->setModelType(PW::ModelType::LUCENCY);
    }else {
        ui->paintWidget->setModelType(PW::ModelType::ERASER);
    }
}

void CbackgroundWidget::onBtnRevokeClicked() {

    AllPropertyWidgetHide();
    ui->paintWidget->RevocationSingleStep();
}

void CbackgroundWidget::onBtnBgClicked() {

    colorWidget->hide();

    QSize PloygonSize= selectBgColorWidget->size();
    QScreen *screen = QGuiApplication::primaryScreen();
    QRect screenRect = screen->availableVirtualGeometry();
    QPoint PloygonFrameGeometry = paintListWidget->pos();
    if (paintListWidget->pos().y() > screenRect.height()/2) {
        selectBgColorWidget->setGeometry(PloygonFrameGeometry.x()+200,paintListWidget->pos().y()-10-PloygonSize.height(),PloygonSize.width(),PloygonSize.height());
    }else {
        selectBgColorWidget->setGeometry(PloygonFrameGeometry.x()+200,paintListWidget->pos().y()+66,PloygonSize.width(),PloygonSize.height());
    }

    selectBgColorWidget->setHidden(!selectBgColorWidget->isHidden());
}

void CbackgroundWidget::onBtnExitClicked() {
    AllPropertyWidgetHide();

    setCursor(QCursor(QPixmap("")));

    ui->paintWidget->ClearPaint();

    paintListWidget->hide();
    ui->w->setStyleSheet("#w{background-color: rgb(43, 144, 25, 0)}");

    emit stopDraw();
}

void CbackgroundWidget::onBtnClearClicked() {
    ui->w->setStyleSheet("#w{background-color: rgb(190,190,190,1)}");

    AllPropertyWidgetHide();

    ui->paintWidget->ClearPaint();
}

void CbackgroundWidget::onBtnSelectPenClicked() {
    selectBgColorWidget->hide();

    colorWidget->isSelectNitePen = false;
    QSize PloygonSize= colorWidget->size();
    QScreen *screen = QGuiApplication::primaryScreen();
    QRect screenRect = screen->availableVirtualGeometry();
    QPoint PloygonFrameGeometry = paintListWidget->pos();
    if (paintListWidget->pos().y() > screenRect.height()/2) {
        colorWidget->setGeometry(PloygonFrameGeometry.x(),paintListWidget->pos().y()-10-PloygonSize.height(),PloygonSize.width(),PloygonSize.height());
    }else {
        colorWidget->setGeometry(PloygonFrameGeometry.x(),paintListWidget->pos().y()+66,PloygonSize.width(),PloygonSize.height());
    }

    colorWidget->setHidden(!colorWidget->isHidden());
}

void CbackgroundWidget::onBtnSelectNitePenClicked() {
    selectBgColorWidget->hide();

    colorWidget->isSelectNitePen = true;
    QSize PloygonSize= colorWidget->size();
    QScreen *screen = QGuiApplication::primaryScreen();
    QRect screenRect = screen->availableVirtualGeometry();
    QPoint PloygonFrameGeometry = paintListWidget->pos();
    if (paintListWidget->pos().y() > screenRect.height()/2) {
        colorWidget->setGeometry(PloygonFrameGeometry.x()+100,paintListWidget->pos().y()-10-PloygonSize.height(),PloygonSize.width(),PloygonSize.height());
    }else {
        colorWidget->setGeometry(PloygonFrameGeometry.x()+100,paintListWidget->pos().y()+66,PloygonSize.width(),PloygonSize.height());
    }

    colorWidget->setHidden(!colorWidget->isHidden());
}

void CbackgroundWidget::showPaintWindow() {
//    paintListWidget->setGeometry(200, 300, 400, 56);
    paintListWidget->show();
    ui->w->setStyleSheet("#w{background-color: rgb(190,190,190,1)}");

    setCursor(QCursor(QPixmap(":/new/icon/image/icon_brush_click.png")));
}

void CbackgroundWidget::saveImg() {
    qDebug("saveImg Action");
}

void CbackgroundWidget::stopShare() {
    qDebug("stopShare Action");

    AllPropertyWidgetHide();
    ui->w->setStyleSheet("#w{background-color: rgb(43, 144, 25, 0)}");
    setCursor(QCursor(QPixmap("")));
    paintListWidget->hide();
    ui->paintWidget->hide();

    emit stopDraw();

    QMessageBox:: StandardButton result = QMessageBox::information(NULL, "Note", "Are you sure to stop?",
                             QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    switch (result)
    {
    case QMessageBox::Yes:
        qDebug("click Yes");
        paintListWidget->close();
        this->close();
        break;
    case QMessageBox::No:
        qDebug("click No");
        paintListWidget->show();
        ui->paintWidget->show();
        ui->w->setStyleSheet("#w{background-color: rgb(190,190,190,1)}");
        setCursor(QCursor(QPixmap(":/new/icon/image/icon_brush_click.png")));
        break;
    default:
        break;
    }
}

void CbackgroundWidget::entershareBtn() {
    if (ui->horizontalView->isHidden()) {
        ui->PropertyWidget->setMinimumHeight(59);
        ui->PropertyWidget->setMaximumHeight(59);
        ui->widget->setMinimumHeight(59);
        ui->widget->setMaximumHeight(59);
        ui->horizontalView->show();
    }else {
        ui->PropertyWidget->setMinimumHeight(15);
        ui->PropertyWidget->setMaximumHeight(15);
        ui->widget->setMinimumHeight(15);
        ui->widget->setMaximumHeight(15);
        ui->horizontalView->setHidden(true);
    }
}

void CbackgroundWidget::changePenColor() {
    setCursor(QCursor(QPixmap(":/new/icon/image/icon_brush_click.png")));

    if(colorWidget->isSelectNitePen) {
        paintListWidget->setNitePenSelected();
        ui->paintWidget->setModelType(PW::ModelType::NITELINE);
    }else {
        paintListWidget->setPenSelected();
        ui->paintWidget->setModelType(PW::ModelType::FREELINE);
    }

    QPen pen(colorWidget->GetColor(),colorWidget->GetSize());
    ui->paintWidget->setPen(pen);
}

void CbackgroundWidget::changePenLineSize() {
    setCursor(QCursor(QPixmap(":/new/icon/image/icon_brush_click.png")));

    if(colorWidget->isSelectNitePen) {
        paintListWidget->setNitePenSelected();
        ui->paintWidget->setModelType(PW::ModelType::NITELINE);
    }else {
        paintListWidget->setPenSelected();
        ui->paintWidget->setModelType(PW::ModelType::FREELINE);
    }

    QPen pen(colorWidget->GetColor(),colorWidget->GetSize());
    ui->paintWidget->setPen(pen);
}

void CbackgroundWidget::c_changeDrawBg(QString q) {
    colorStr = q;

    emit changeBgColor();
}

void CbackgroundWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPen p;
    p.setWidth(5);
    p.setColor(QColor(67, 129, 255));
    painter.setPen(p);
    painter.drawLine(0, 0, this->width() - 1, 0);
    painter.drawLine(0, 0, 0, this->height() - 1);
    painter.drawLine(this->width() - 1, 0, this->width() - 1, this->height() - 1);
    painter.drawLine(0, this->height() - 1, this->width() - 1, this->height() - 1);
}

CbackgroundWidget::~CbackgroundWidget()
{
    delete ui;

    if(paintListWidget != nullptr)
        delete paintListWidget;
    paintListWidget = nullptr;

    if(colorWidget != nullptr)
        delete colorWidget;
    colorWidget = nullptr;

    if(selectBgColorWidget != nullptr)
        delete selectBgColorWidget;
    selectBgColorWidget = nullptr;
}
